var express = require('express');
var app = express();

var fs = require('fs');

app.set('port', (process.env.PORT || 8000));
app.use(express.static(__dirname + '/app'));
console.log(__dirname);

copyFolder("bower_components","app/_lib");

app.get('/', function(request, response) {
   response.sendfile('index.html', {root: __dirname})
});

app.listen(app.get('port'), function() {
  console.log("Node app is running at 127.0.0.1:" + app.get('port'));
});

//Copy the bower components into the lib for the app use
function copyFolder(path, dest) {
   var fileList = [],
       folderList = [],
       walk = function(path,dest) {
           files = fs.readdirSync(path);
           files.forEach(function(item) {
               var tmpPath = path + '/' + item,
                   tmpDestPath = dest + '/' + item,
                   stats = fs.statSync(tmpPath);

               if (stats.isDirectory()) {
                   if (!fs.existsSync(tmpDestPath)) {
                       fs.mkdirSync(tmpDestPath);
                   }
                   walk(tmpPath,tmpDestPath);
               } else {
                   var readable = fs.createReadStream(tmpPath),
                       writable = fs.createWriteStream(tmpDestPath);   
                   readable.pipe(writable);
               }
           });
       };
   if (!fs.existsSync(dest)) {
       fs.mkdirSync(dest);
   }
   walk(path,dest);
}


